package org.NooLab.itexx.comm.tcp.box;

import java.util.Observable;
import java.util.Observer;

import org.NooLab.itexx.comm.intf.CommboxIntf;
import org.NooLab.itexx.comm.tcp.box.components.TcpClient;
import org.NooLab.itexx.comm.tcp.box.components.TcpMessageHandlerFactory;
import org.NooLab.itexx.comm.tcp.box.components.TcpServer;
import org.NooLab.itexx.comm.tcp.box.infra.*;


 	



public class TcpBox extends Observable implements TransportBoxIntf, GlueTcpletHandlerIntf{

	DataSenderIntf tcpSender;
	DataReceiverIntf tcpReceiver;
	
	TcpMessageHandlerFactory msgFactory ; 
	
	ServerMsgHandlerIntf serverMsg ;
	ClientMsgHandlerIntf clientMsg ;
	
	TransportsInformerIntf transportInformer;
	private CommboxIntf parent;
	
	
	// ========================================================================	
	public TcpBox(){
		msgFactory = new TcpMessageHandlerFactory();
		
		serverMsg = msgFactory.getServerMsgHandler();
		clientMsg = msgFactory.getClientMsgHandler();
	}
	// ========================================================================
	
	public TcpBox(CommboxIntf commboxIntf) {
		parent = commboxIntf;
		
		msgFactory = new TcpMessageHandlerFactory();
		
		serverMsg = msgFactory.getServerMsgHandler();
		clientMsg = msgFactory.getClientMsgHandler();

	}

	@Override
	public void deleteObservers(){
		
		super.deleteObservers() ;
		tcpReceiver.deleteObservers() ;
		
		if (parent!=null){
			parent.deleteParentObservers() ;
		}
	}
	
	
	public void addTcpObserver( Object obsobj) throws Exception{
		
		if ((obsobj instanceof Observer)==false){
			throw(new Exception("provided object must be a sub-class of Observer !"));
		}
		
		super.addObserver((Observer) obsobj);
		tcpReceiver.setObserver((Observer) obsobj) ;
		
		if (parent!=null){
			parent.addParentObserver((Observer) obsobj);
		}
	}
	
	// ------------------------------------------------------------------------
	
	public DataReceiverIntf createReceiver( int listenport, int ctrlport, Observer observer, String description) throws Exception {
		TcpServer tcpServer;
		int r=0;
	 	
		tcpServer = new TcpServer( this, listenport, ctrlport, observer, serverMsg, description);
		r = tcpServer.activate() ;
		
		if (tcpServer.getSocketState()==0){
			tcpReceiver = (DataReceiverIntf)tcpServer ;
		}else{
			tcpReceiver = null;
		}
		
		return tcpReceiver;
	} 
	
	public DataSenderIntf createSender(  int clientport, int ctrlport, String description ) {
		 
		tcpSender = new TcpClient( clientport, ctrlport, clientMsg, description );
		
		return tcpSender;
	}
	
	
	public void setTransportsInformer( TransportsInformerIntf informer ){
		transportInformer = informer ;
	}

	public TransportsInformerIntf getTransportInformer() {
		return transportInformer;
	}

	public DataSenderIntf getTcpSender() {
		return tcpSender;
	}

	public DataReceiverIntf getTcpReceiver() {
		return tcpReceiver;
	}

	public TcpMessageHandlerFactory getMsgFactory() {
		return msgFactory;
	}

	public ServerMsgHandlerIntf getServerMsg() {
		return serverMsg;
	}

	public ClientMsgHandlerIntf getClientMsg() {
		return clientMsg;
	}
	
	
	
}
